﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HeerCards
{
    public class PoolGetInfo
    {
        public HCCard Card;
        public HCLevel Level;
    }

    public class HCPool
    {
        public HCPool()
        {
            Message = "";
            TenMessage = "";
            Cost = 0;
            useImage = true;
            CostItem = "";
        }

        public string PoolName { get; set; }
        public string Message { get; set; }
        public string TenMessage { get; set; }
        public int Cost { get; set; }
        public string CostItem { get; set; }
        public bool useImage { get; set; }
        public bool drwaText { get; set; }//绘制文本
        public List<HCLevel> LevelCollections { get; set; } = new List<HCLevel>();

        public void AddLevel(HCLevel level)
        {
            LevelCollections.Add(level);
        }

        public void AddCard(HCCard hCCard, HCLevel hLevel)
        {
            LevelCollections.Find((HCLevel level) => level.Name == hLevel.Name)?.addCard(hCCard);
        }

        public void AddCard(HCCard hCCard, string levelName)
        {
            LevelCollections.Find((HCLevel level) => level.Name == levelName)?.addCard(hCCard);
        }

        public List<HCLevel> GetLevels()
        {
            return LevelCollections;
        }

        public HCCard GetCardWithGuid(int guid)
        {
            foreach (var item in LevelCollections)
            {
                foreach (var card in item.Cards)
                {
                    if (card.GUID == guid)
                    {
                        return card;
                    }
                }
            }
            return null;
        }

        public void ReplaceCardWithGuid(int guid, HCCard newCard)
        {
            foreach (var item in LevelCollections)
            {
                var r = item.Cards.Find((HCCard card) => card.GUID == guid);
                if (r == null) continue;
                r.Name = newCard.Name;
                r.Description = newCard.Description;
                r.ExtraProbability = newCard.ExtraProbability;
                r.LocalImagePath = newCard.LocalImagePath;
                return;
            }
        }

        public void RemoveCardWithGuid(int guid)
        {
            foreach (var item in LevelCollections)
            {
                var c = item.Cards.Find((HCCard card) => card.GUID == guid);
                if (c != null)
                {
                    item.Cards.Remove(c);
                    return;
                }
            }
        }

        public List<PoolGetInfo> GetCard(int number)
        {
            var returnList = new List<PoolGetInfo>();
            //分配所有ratio范围
            var count = 0;
            foreach (var item in LevelCollections)
            {
                item.min = count;
                count += item.Ratio;
                item.max = count;
            }

            if (count == 0)
            {
                throw new Exception("卡池所有等级的占比均为0，无法进行抽卡");
            }
            //得到最大范围
            var maxRatio = LevelCollections[LevelCollections.Count - 1].max;

            //获得绝对ratio
            var sns = Tool.StrangeRanmdom(number);
            foreach (var item in sns)
            {
                var nowRatio = maxRatio * item;
                HCLevel thisLevel = null;
                List<HCCard> cardList = new List<HCCard>();
                //取出对应范围
                foreach (var tLevel in LevelCollections)
                {
                    if ((int)nowRatio < tLevel.max && (int)nowRatio >= tLevel.min)
                    {
                        //必定存在
                        thisLevel = tLevel;
                    }
                }

                if (thisLevel != null)
                    returnList.Add(new PoolGetInfo() { Level = thisLevel, Card = thisLevel.getCard() });
            }
            if (number >= 10)
            {
                var s = this.LevelCollections.Find((HCLevel evel) => evel.miniG == true);
                if (s != null)
                {
                    if (returnList.Find((PoolGetInfo info) => info.Level == s) == null)
                    {
                        returnList[number - 1] = new PoolGetInfo() { Level = s, Card = s.getCard() };
                    }
                }
            }
            return returnList;
        }
    }
}